<?php
/*******************************************************************************
 *
 *  Name:  Incident Model
 *
 *  Date: 4/1/2012
 *  
 *  Description: This model allows access to the Incident table in our database.
 *  
 *  Author: Hayden Waisanen
 *  
 *  Todo:
 *     NONE
 *
 *  Modification History:  
 * 
 *   Date         Name
 *  ----------   --------------
 *  2012-04-26   HAYDEN WAISANEN
 *  2012-04-25   HAYDEN WAISANEN
 *  2012-04-25   DAVID JARMAN
 *  2012-04-22   HAYDEN WAISANEN
 *  2012-04-22   HAYDEN WAISANEN
 *  2012-04-17   HAYDEN WAISANEN
 *  2012-04-17   DAVID JARMAN
 *  2012-04-17   HAYDEN WAISANEN
 *  2012-04-09   HAYDEN WAISANEN
 * 
 *****************************************************************************/
?>
<?php
class Incident_model extends CI_Model {

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
        $this->load->database();
    }
    
    function get($IncidentID)
    {
        $query = $this->db->get_where('Incident', array('IncidentID' => $IncidentID));
        return $query->result_array();
    }

    function getTeamIncidences($IRTeam)
    {
        $query = $this->db->get_where('Incident', array('AssignedTeam' => $IRTeam));
        return $query->result_array();
    }

    function get_where($params)
    {
        $query = $this->db->get_where('Incident', $params);
        return $query->result_array();
    }

    function getToday($IRTeam = '')
    {
       $irteamsearch = '';
       if($IRTeam != '')
          $irteamsearch = "AND AssignedTeam ='".$IRTeam."'";

        $query = $this->db->query('SELECT * FROM Incident WHERE (DATEDIFF(WorkStarted, NOW()) <= 0 AND DATEDIFF(IF(WorkFinished IS NULL, NOW(), WorkFinished),NOW())>=0)'.$irteamsearch );
        return $query->result_array();
    }
    function getRange($start,$end,$irteam='All')
    {
       $irteamsearch = '';

       //Search for all of irteam is all
       if($irteam != 'All')
          $irteamsearch = " AND AssignedTeam = '$irteam'";

       $querystring = 
       "SELECT * 
FROM Incident 
WHERE 
((DATEDIFF(WorkStarted,str_to_date('$start','%m-%d-%Y'))) >0 AND 
 (DATEDIFF(IF(WorkFinished IS NULL, WorkStarted, WorkFinished),str_to_date('$end','%m-%d-%Y'))) < 0) OR
 ((DATEDIFF(WorkStarted,str_to_date('$start','%m-%d-%Y'))) >0 AND 
 (DATEDIFF(WorkStarted,str_to_date('$end','%m-%d-%Y'))) < 0) OR
 ((DATEDIFF(WorkFinished,str_to_date('$start','%m-%d-%Y'))) >0 AND 
 (DATEDIFF(WorkFinished,str_to_date('$end','%m-%d-%Y'))) < 0)OR
 ((DATEDIFF(WorkStarted,str_to_date('$start','%m-%d-%Y'))) <0 AND 
 (DATEDIFF(WorkFinished,str_to_date('$end','%m-%d-%Y'))) > 0)".$irteamsearch;
        $query = $this->db->query($querystring);
        return $query->result_array();
    }

    function getAll()
    {
       $query = $this->db->get('Incident');
       return $query->result_array();
    }

    function insert($fields)
    {
        //Check if it already exists
        
        $query = $this->db->get_where('Incident', array('IncidentID' => $fields['IncidentID']));
        if( $query->num_rows() > 0 )
           return false;
           

        $this->db->insert('Incident', $fields);
        return true;
    }

    function update($fields)
    {
       $IncidentID = $fields['IncidentID'];
       unset($fields['IncidentID']);
        $this->db->update('Incident', $fields, array('IncidentID' => $IncidentID));
    }
    
    function delete($fields)
    {
         $this->db->where('IncidentID', $fields['IncidentID']);
         $this->db->delete('Incident'); 
    }
}
?>
